<?php
/*
 * @Author: your name
 * @Date: 2021-12-07 09:50:02
 * @LastEditTime: 2021-12-22 15:18:53
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \property\app\Http\Controllers\find_shop\User.php
 */

namespace App\Http\Controllers\find_shop;

use Illuminate\Support\Facades\Request;
// use App\Models\admin_model\Admins as AdminsDB;
use App\Models\admin_model\FindShopCollectionHistory;

class CollectionHistory extends BaseController
{
    public function  historyList()
    {
        $limitStr = '';
        if (Request::has('page') && Request::has('pageSize')) {
            $limitStr =   limit_joint(Request::input('page'), Request::input('pageSize'));
        }
        $user =  new User;
        if($user->checkToken(false)){    //已经登录的情况下
           $openid =   $this->getLoginMsg();
           return $this->create(200,'查询成功', FindShopCollectionHistory::historyUserList( $openid, $limitStr)) ;
        }else  if (Request::has('history_array')) { //没有登录的情况下
            $history_array = Request::input('history_array');
            $storeIdStr = '';
            $housingIdStr = '';
            $buildingIdStr = '';
            $orderByStr = '';
            $data = [];
            for($i=0;$i<count($history_array);$i++){
                if($history_array[$i]['type'] == 'store'){
                    $storeIdStr .= $history_array[$i]['id'].',';
                }else if($history_array[$i]['type'] == 'housing'){
                    $housingIdStr .= $history_array[$i]['id'].',';
                }else if($history_array[$i]['type'] == 'building'){
                    $buildingIdStr .= $history_array[$i]['id'].',';
                }
                $orderByStr .=   '"'.$history_array[$i]['type'].'_'.$history_array[$i]['id'].'",';
            }
            if(($storeIdStr || $housingIdStr || $buildingIdStr   ) &&$orderByStr){  //两个都有的情况下
                $storeIdStr =  !$storeIdStr?'""':substr($storeIdStr,0,-1);
                $housingIdStr = !$housingIdStr?'""':substr($housingIdStr,0,-1);
                $buildingIdStr  =!$buildingIdStr?'""':substr(  $buildingIdStr ,0,-1);
                $orderByStr = substr($orderByStr,0,-1);
                $data = FindShopCollectionHistory::storeHousingHistory($housingIdStr,$storeIdStr,$buildingIdStr,$orderByStr,$limitStr);
            }
            // else if($storeIdStr){      //历史记录只有店铺
            //     $storeIdStr = substr($storeIdStr,0,-1);
            //     $data = FindShopCollectionHistory::storeHistory($storeIdStr,$limitStr);
            // }else if($housingIdStr){    //历史记录只有房源
            //     $housingIdStr = substr($housingIdStr,0,-1);
            //     $data = FindShopCollectionHistory::housingHistory($housingIdStr,$limitStr);
            // }

            return $this->create(200,'查询成功',$data);
        }
    }
    public function historyAdd()
    {
        if (Request::has('history_array')) {
            $history_array =  Request::input('history_array');
            $openid = $this->getLoginMsg();
            $concatStr = '';
            $array = [];
            for($i=count($history_array)-1;$i>=0;$i--){
                $tmp = [];
                $tmp['type'] = 1;
                $tmp['target_id']   =  $history_array[$i]['id'];
                $tmp['table']       =  $history_array[$i]['type'];
                $tmp['user_openid'] =  $openid;
                $concatStr .=  '"'.$tmp['table'].'_'. $tmp['target_id'].'",';
                $array[] = $tmp;
            }
            if( $concatStr){
                FindShopCollectionHistory::historyDel( $openid,substr( $concatStr,0,-1));
            }
            FindShopCollectionHistory::collectionHistoryAdd($array);
            FindShopCollectionHistory::historyDelOver($openid,5);

            return $this->create(200,'添加成功');
        }
    }

    public function historyAddOne(){
        $user =  new User;
        if ($user->checkToken(false) && Request::has('history')) {
            $openid  = $this->getLoginMsg();
            $history = Request::input('history');
            $tmp = [];
            $tmp['type'] = 1;
            $tmp['target_id']   =  $history['id'];
            $tmp['table']       =  $history['type'];
            $tmp['user_openid'] =  $openid;
            FindShopCollectionHistory::historyDel( $openid,'"'.$history['type'].'_'.$history['id'].'"');
            FindShopCollectionHistory::collectionHistoryAdd($tmp);
            FindShopCollectionHistory::historyDelOver($openid,5);
            return $this->create(200,'添加成功');
        }else{
            return $this->create(100,'登录过期');
        }
    }
   
    public function collectionList(){
        $limitStr = '';
        $openid  = $this->getLoginMsg();
        if (Request::has('page') && Request::has('pageSize')) {
            $limitStr =   limit_joint(Request::input('page'), Request::input('pageSize'));
        }
         return $this->create(200,'添加成功',FindShopCollectionHistory::collectionList($openid,$limitStr));
    }


    public function collectionOne(){
           if(Request::has('collection') && Request::has('table') && Request::has('target_id')){
               $collect = Request::input('collection');
               $target_id = Request::input('target_id');
               $table = Request::input('table');
               $openid = $this->getLoginMsg();
                $collect?FindShopCollectionHistory::insertUpdated(
                    insertUpdatedStr(
                        array(
                            'target_id'=> $target_id,
                            'table' => $table,
                            'user_openid'=> $openid,
                            'type'=>2
                        )
                    )
                ):FindShopCollectionHistory::collectionDel( $openid,$table,$target_id);

                return $this->create(200,'修改成功');
           }
    }



}
